package com.home766.service.impl;

import com.home766.dao.CasesDao;
import com.home766.dao.ShopsDao;
import com.home766.dto.ShopsDto;
import com.home766.entity.AccountNumber;
import com.home766.entity.Shops;
import com.home766.service.AccountNumberService;
import com.home766.service.ShopsService;
import net.biz.Page;
import net.biz.Pageable;
import net.biz.dao.RoleDao;
import net.biz.entity.Admin;
import net.biz.entity.Area;
import net.biz.entity.Role;
import net.biz.service.AdminService;
import net.biz.service.impl.BaseServiceImpl;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Service - 门店
 * 
 * @author Ricky
 * @version 1.0
 */
@Service("shopsServiceImpl")
@Transactional
public class ShopsServiceImpl extends BaseServiceImpl<Shops, Long> implements ShopsService{
	@Autowired
	private ShopsDao shopsDao;
	
	@Resource(name = "roleDaoImpl")
	public RoleDao roleDaoImpl;
	
	
	@Resource(name="adminServiceImpl")
	private AdminService adminService;
	
	@Resource(name="accountNumberServiceImpl")
	private AccountNumberService accountNumberService;
	
	@Value("${default_email}")
	private String DEFAULT_EMAIL;
	
	@Autowired
	public void setBaseDao(CasesDao casesDao) {
		super.setBaseDao(shopsDao);
	}
	@Override
	public Page<Shops> getShopsList(Shops shops, Pageable pageable, String properties) {
		// TODO Auto-generated method stub
		return shopsDao.getShopsList(shops, pageable, properties);
	}
	@Override
	public void updateShops(Shops shops) {
		// TODO Auto-generated method stub
		Shops shoptem = shopsDao.find(shops.getId());
		if(!shoptem.getShopsName().equals("总部售后")){
			shoptem.setShopsName(shops.getShopsName());
			shoptem.setShopsLevel(shops.getShopsLevel());
		}
		shoptem.setAddress(shops.getAddress());
		shoptem.setContactP(shops.getContactP());
		shoptem.setContactT(shops.getContactT());
		shoptem.setStoreType(shops.getStoreType());
		shoptem.setStoreScale(shops.getStoreScale());
		shoptem.setTaxpayerType(shops.getTaxpayerType());
		shoptem.setProvinceId(shops.getProvinceId());
		shoptem.setCityId(shops.getCityId());
		shoptem.setAreaId(shops.getAreaId());
		shoptem.setGpsX(shops.getGpsX());
		shoptem.setGpsY(shops.getGpsY());
		shoptem.setRanges(shops.getRanges());
		shoptem.setShopsNature(shops.getShopsNature());
		shopsDao.merge(shoptem);
	}
	@Override
	public List<Area> getShopsArea() {
		// TODO Auto-generated method stub
		return shopsDao.getShopsArea();
	}
	@Override
	public List<Area> getShopsAreaChildren(Long provinceId) {
		// TODO Auto-generated method stub
		return shopsDao.getShopsAreaChildren(provinceId);
	}
	@Override
	public List<Shops> getShopsByAreaId(Long areaId, boolean limit, Long shopsNature) {
		// TODO Auto-generated method stub
		return shopsDao.getShopsByAreaId(areaId,limit,null,null,shopsNature);
	}
	@Override
	public List<ShopsDto> getShopsList(Shops shops) {
		// TODO Auto-generated method stub
		return shopsDao.getShopsList(shops);
	}
	@Override
	public List<Area> getShopsAreaChildrens(Long cityId) {
		// TODO Auto-generated method stub
		return shopsDao.getShopsAreaChildrens(cityId);
	}
	@Override
	public Shops saveShops(Shops shops, String name, String password) {
		// TODO Auto-generated method stub
		Role role=roleDaoImpl.findByName(name);
		AccountNumber accountNumber = accountNumberService.getNewest();
		if(shops.getShopsName().equals("总部售后")){
			accountNumber.setBaseNumber(10000);
			//默认A级门店
			shops.setShopsLevel(172L);
		}
		save(shops);
		if(accountNumber.getBaseNumber()==null){
			accountNumber.setBaseNumber(10001);
			accountNumberService.save(accountNumber);
		}
		boolean flge = adminService.usernameExists(accountNumber.getBaseNumber().toString());
		while(flge==true){
			accountNumber.setBaseNumber(accountNumber.getBaseNumber()+1);//账号存在就自增
			accountNumberService.update(accountNumber);
			flge = adminService.usernameExists(accountNumber.getBaseNumber().toString());
		}
		if(role!=null){
			Admin admin=new Admin();
			admin.setUsername(accountNumber.getBaseNumber().toString());
			admin.setPassword(DigestUtils.md5Hex(password));
			admin.setIsLocked(false);
			admin.setLoginFailureCount(0);
			admin.setLockedDate(null);
			admin.setLoginDate(null);
			admin.setLoginIp(null);
			admin.setOrders(null);
			admin.setShops(shops);
			admin.setEmail(DEFAULT_EMAIL);
			admin.setIsEnabled(true);
			admin.setIsLocked(false);
			Set<Role> roles=null;
			if(role!=null){
				roles=new HashSet<Role>();
				roles.add(role);
			}
			admin.setRoles(roles);
			admin.setIsSystem(false);
			adminService.save(admin);
			shops.setAdmin(admin);
			save(shops);
		}
		return shops;
	}
	@Override
	public Page<Shops> getShopsReceptionList(Shops shops, Pageable pageable) {
		// TODO Auto-generated method stub
		return shopsDao.getShopsReceptionList(shops, pageable);
	}
	@Override
	public Shops getSingleShops(Long adminId) {
		// TODO Auto-generated method stub
		return shopsDao.getSingleShops(adminId);
	}
	@Override
	public Shops getServiceTotalShops() {
		// TODO Auto-generated method stub
		return shopsDao.getServiceTotalShops();
	}
	@Override
	public List<Shops> getShopsLevel(Long province) {
		// TODO Auto-generated method stub
		return shopsDao.getShopsLevel(province);
	}
}
